module.exports = class extends baseLogic {
    constructor() {
        super();
    }

    static init() {
        return new this();
    }

    async getItems(FindFilterModel, CurPage, PageSize, Sort,moduleid, UserInfo) {
        let Result = {};

        Result.CurPage = CurPage;
        Result.PageSize = PageSize;
        let FindFilter=await this.func.degree.getDegree(moduleid,UserInfo,{orgId:'RuleOrgID'});

        if (FindFilterModel.RuleName) {
            FindFilter.RuleName = {
                '$like': '%' + FindFilterModel.RuleName + '%'
            };
        }

        if (!isNaN(FindFilterModel.RuleOrgID)) {
            let ChildrensID_array =await this.func.orgStruct.getChildrensID(FindFilterModel.RuleOrgID);
            ChildrensID_array.push(Number(FindFilterModel.RuleOrgID));
            FindFilter.RuleOrgID = ChildrensID_array;
        }


        let data = await this.DB.iframe_rule.Access.findAndCount({
            where: FindFilter,
            include:[
                {
                    association:this.DB.iframe_rule.Access.belongsTo(this.DB.iframe_orgstruct.Access,{foreignKey:'RuleOrgID'}),
                    attributes:['OS_Name'],
                    model:this.DB.iframe_orgstruct.Access,
                    required: false
                }
            ],
            order: [
                Sort.split('|')
            ],
            offset: (CurPage - 1) * PageSize,
            limit: PageSize,
            raw:true
        });



        Result.RsCount = data.count;
        Result.DataList = data.rows;
        Result.OrgStruct = await this.func.orgStruct.getPartnersItemsTreeData(moduleid,UserInfo); //根据各自的数据可视化程度 获取用户可见的组织结构的树形结构数据
        return Result;
    }

    async getItem(id,UserInfo) {
        let ret = {};
        ret.Model=await this.DB.iframe_rule.Access.findOne({
            where:{
                RuleID:id
            },
            raw:true
        });

        ret.UserRuleAbles = await this.func.curUserInfo.getUserRuleAble(UserInfo.UserRule,UserInfo.UserPartnersID);
        let M = [];
        ret.UserRuleAbles.map(function(it) {
            if (it.M) {
                M.push(it.M);
            }
        });
        let SysModules = await this.DB.iframe_system_module.Access.findAll({
            where: {
                ModuleID: M
            },
            order: [
                ['Sort', 'ASC']
            ],
            raw:true
        });

        ret.SysModules = SysModules;

        return ret ;
    }

    async insertOrUpdate(body,userInfo){

        var RuleID = body.RuleID ? Number("0" + body.RuleID) : 0;
        var Ret = await this.DB.iframe_rule.Access.findOrInitialize({
            where: {
                RuleID: RuleID
            },
            defaults: {
                RulePartnersID:body.RulePartnersID || userInfo.UserPartnersID
            }
        });
        var Model = Ret[0];
        Model.RuleAble = body.treeval;
        Model.RuleName = body.RuleName;
        Model.RuleInfo = body.RuleInfo;
        Model.RulePartnersID=body.RulePartnersID || userInfo.UserPartnersID;
        Model.RuleDataDegreeCode=body.RuleDataDegreeCode;

        Ret = await Model.save();
        return Model;
    }

    async del(keys){
        return  await this.DB.iframe_rule.Access.destroy({
            where: {
                RuleID: keys
            }
        });
    }

    async getItemsByPartnersID(RuleOrgID){
        return  await this.DB.iframe_rule.Access.findAll({
            where: {
                RuleOrgID: RuleOrgID
            },
            raw:true
        });
    }
}
